<?php

class Users extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$this->itemlist();
	}
	
	function itemlist()
	{
		$this->load->model("user_model");
		$this->load->model("person_model");
		$this->load->model("department_model");
				
				
		$data['persons'] = $this->person_model->get_persons();
		$data['departments'] = $this->department_model->get_departments();
		$data['content'] = $this->load->view("users/list_view", $data, true);		
		
		$data['title'] = array( "Admin", "Security", "Users" );
		$this->load->view("main_view", $data);		
	}
	
	function create()
	{
		$this->load->model("user_model");
	
		$data['employee_id'] = $_POST['employee_id'];
		$data['username'] = $_POST['username'];
		$data['password'] = $_POST['password'];
		$data['department_id'] = $_POST['department_list'];
	
		if($_POST['person_list'])
		{
			$data['person_id'] = $_POST['person_list'];
		}
		else
		{
			$this->load->model("person_model");
			$data2['first_name'] = $_POST['first_name'];
			$data2['last_name'] = $_POST['last_name'];
			$data2['company_id'] = 1; //TODO: change to profoods
			$data['person_id'] = $this->person_model->create($data2);
		}
		
		echo $this->user_model->create($data);
	}
	
	function delete($id)
	{
		$this->load->model("user_model");
		$this->user_model->delete($id);
	}
	
	function get($page, $search_by = null, $search_value = null)
	{
		$this->load->model("user_model");
		echo json_encode($this->user_model->get_users($page, $search_by, $search_value));
	}
}